function countdown(minutes) {
    var seconds = 60;
    var mins = minutes
    function tick() {
        var counter = $("#counter");
        var current_minutes = mins-1
        seconds--;
        $("#countermin").text(current_minutes.toString());
        $("#countersec").text((seconds < 10 ? "0" : "") + String(seconds));
        if( seconds > 0 ) {
            setTimeout(tick, 1000);
        } else {
            if(mins > 1){
                countdown(mins-1);
            }else{
            	counter.text(StartNightAudit);
            	counter.attr("onclick","toNightReport()");
            }
        }
    }
    tick();
}

function toNightReport(){
	document.location.href ="/nightaudit/nightreport.jsp";
}

function loadNightRePort(){
	var data = {func : "getNightAuditReport"};
	var url = "/nightauditservice";
	simpleservice(url,data,fillNightAuditReport);
}

function fillNightAuditReport(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	$("[name=nightauditdate]").html(" "+new Date(objArray["date"]).addDate(-1).getFormatedString());
	
	$("#amount1").html(objArray["res"].length);
	$("#amount2").html(objArray["counter"].length);
	$("#amount3").html(objArray["guest"].length);
	
	if(objArray["res"].length == 0){
		$("#table1").append("<tr><td colspan=\"8\">No Data</td></tr>");
	}else{
		var sumtext = "";
		for(var i=0;i<objArray["res"].length;i++){
			sumtext += "<tr refid=\""+objArray["res"][i]["resId"]+"\">"+
							"<td>"+(i+1)+"</td>"+
							"<td>"+objArray["res"][i]["resId"]+"</td>"+
							"<td>"+objArray["res"][i]["userName"]+"</td>"+
							"<td>"+objArray["res"][i]["roomtypeName"]+"</td>"+
							"<td>"+objArray["res"][i]["roomName"]+"</td>"+
							"<td>"+new Date(objArray["res"][i]["checkInTime"]).getFormatedString()+" - "+
							new Date(objArray["res"][i]["checkOutTime"]).getFormatedString()+"</td>"+
							"<td>"+objArray["res"][i]["pay"]+"</td>"+
							"<td><input name=\"checkbox1\" type=\"checkbox\" value=\"\"></td>"+
						"</tr>";
		}
		$("#table1").append(sumtext);
	}
	
	if(objArray["guest"].length == 0){
		$("#table2").append("<tr><td colspan=\"7\">No Data</td></tr>");
	}else{
		var sumtext = "";
		for(var i=0;i<objArray["guest"].length;i++){
			sumtext += "<tr refid=\""+objArray["res"][i]["resId"]+"\">"+
							"<td>"+(i+1)+"</td>"+
							"<td>"+objArray["guest"][i]["resId"]+"</td>"+
							"<td>"+objArray["guest"][i]["userName"]+"</td>"+
							"<td>"+objArray["guest"][i]["roomtypeName"]+"</td>"+
							"<td>"+objArray["guest"][i]["roomName"]+"</td>"+
							"<td>"+new Date(objArray["guest"][i]["checkInTime"]).getFormatedString()+" - "+
							new Date(objArray["guest"][i]["checkOutTime"]).getFormatedString()+"</td>"+
							"<td><input name=\"checkbox2\" type=\"checkbox\" value=\"\"></td>"+
						"</tr>";
		}
		$("#table2").append(sumtext);
	}
	
	if(objArray["counter"].length == 0){
		$("#table3").append("<tr><td colspan=\"8\">No Data</td></tr>");
	}else{
		var sumtext = "";
		for(var i=0;i<objArray["counter"].length;i++){
			sumtext += "<tr refid=\""+objArray["counter"][i]["counterId"]+"\">"+
							"<td>"+(i+1)+"</td>"+
							"<td>"+objArray["counter"][i]["counterName"]+"</td>"+
							"<td>"+objArray["counter"][i]["lastUserName"]+"</td>"+
							"<td>"+objArray["counter"][i]["LastUserId"]+"</td>"+
							"<td style=\"text-align:right;padding-right:15px\">"+objArray["counter"][i]["cash"]+" / "+objArray["counter"][i]["cheque"]+"</td>"+
							"<td><input name=\"checkbox4\" type=\"checkbox\" value=\"\"></td>"+
							"<td><input name=\"checkbox3\" type=\"checkbox\" value=\"\"></td>"+
						"</tr>";
		}
		$("#table3").append(sumtext);
	}
}

function activenightaudit(){
	var data = {func : "activenightaudit"};
	var url = "/nightauditservice";
	simpleservice(url,data,gotosummary);
}

function gotosummary(data){
	document.location.href ="/nightaudit/summaryreport.jsp";
}

function inprogressnightaudit(repeat){
	window.setTimeout(function(){checkintimenightaudit(repeat);}, 60000);
}

function checkintimenightaudit(repeat){
	if(repeat == "true"){
		var data = {func : "inprogressnightaudit"};
		var url = "/nightauditservice";
		simpleservice(url,data,checkresult);
	}
	
}

function checkresult(data){
	if(data == "T"){
		alert("in progress night audit");
	}
}

function getlogedinuser(){
	var data = {func : "getlogedinuser"};
	var url = "/nightauditservice";
	simpleservice(url,data,fillllogedinuser);
}

function fillllogedinuser(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	$("#nightauditdate").html(" "+new Date(objArray["date"]).addDate(-1).getFormatedString());
	var objArray2 = objArray["data"];
	var sumtext = "";
	$("#logedaount").html(objArray2.length);
	for(var i = 0;i< objArray2.length;i++){
			text = "<tr style=\"border: 0px;\"> " +
					"<td align=\"center\"> "+(i+1)+" </td>"+
					"<td>"+objArray2[i]["id"]+"</td>"+
					"<td align=\"left\">"+objArray2[i]["name"]+"</td>"+
					"<td align=\"left\">"+objArray2[i]["email"]+"</td>"+
					"<td align=\"center\">"+objArray2[i]["countername"]+"</td>"+
					"</tr>";
			sumtext += text;
	}
	$("#logeduserid").append(sumtext);
}

function forcelogout(){
	var message = "Are you sure ?";
	confirmDialog(message,gotocountdown);
}

function gotocountdown(){
	document.location.href ="/nightaudit/countdown.jsp";
}

function fillDate(){
	$("#nightauditdate").html(" "+getServerDateTime().addDate(-1).getFormatedString());
}

function loadSummary(){
	var data = {func : "loadsummary"};
	var url = "/nightauditservice";
	simpleservice(url,data,fillSummaryPage);	
}

function fillSummaryPage(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	fillDate();
	$("#room_occ").html(objArray["sumData"]["OccupiedRooms_Rooms"]);
	$("#room_available").html(objArray["sumData"]["AvaliableRooms"]);
	$("#room_checiin").html(objArray["sumData"]["CheckIns_Rooms"]);
	$("#room_checkout").html(objArray["sumData"]["CheckOuts"]);
	$("#room_noshow").html(objArray["sumData"]["NoShows"]);
	$("#room_cancel").html(objArray["sumData"]["Cancellations"]);
	$("#guest_occ").html(objArray["sumData"]["OccupiedRooms_Guest"]);
	$("#guest_checkin").html(objArray["sumData"]["CheckIns_Guest"]);
	
	$("#house1").html(objArray["sumData"]["CheckedOutRoomsMarked"]);
	$("#house2").html(objArray["sumData"]["OccupiedRoomsMarked"]);
	$("#house3").html(objArray["sumData"]["VacantRoomsMarked"]);
	
	$("#revenue_room").html(objArray["sumData"]["RoomRevenue"]);
	$("#revenue_cancel").html(objArray["sumData"]["CancellationRevenue"]);
	$("#revenue_noshow").html(objArray["sumData"]["NoShowRevenue"]);
	$("#revenue_roomcharge").html(objArray["sumData"]["RoomCharge"]);
	$("#revenue_total").html(objArray["sumData"]["OccupiedRooms_Rooms"]);
	
	$("#booking_walkin").html(objArray["sumData"]["WalkIn"]);
	$("#booking_coperate").html(objArray["sumData"]["Coporate"]);
	$("#booking_agent").html(objArray["sumData"]["Agnet"]);
	$("#booking_web").html(objArray["sumData"]["Web"]);
	
	if(objArray['roomTax'] == null){
		$("#table5").append("<tr><td colspan=\"2\">No Data</td></tr>");
	}else{
		var sumtext = "";
		for(var i=0;i<objArray["counter"].length;i++){
			sumtext += "<tr>"+
							"<td>"+(i+1)+"</td>"+
							"<td>"+objArray["counter"][i]["counterName"]+"</td>"+
						"</tr>";
		}
		$("#table5").append(sumtext);
	}

	if(objArray['posTax'] == null){
		$("#table6").append("<tr><td colspan=\"2\">No Data</td></tr>");
	}else{
		var sumtext = "";
		for(var i=0;i<objArray["counter"].length;i++){
			sumtext += "<tr>"+
							"<td>"+(i+1)+"</td>"+
							"<td>"+objArray["counter"][i]["counterName"]+"</td>"+
						"</tr>";
		}
		$("#table6").append(sumtext);
	}
}